package servlet;
import Dao.StudentDao;
import Dao.jdbcTools;
import Model.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@WebServlet("/delOne")
public class StuDel extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        //����Dao��ɾ��api����ɾ������
        try {
            InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
            SqlSession sqlSession = factory.openSession();
            StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
            int delete = studentDao.deleteStudent(Integer.valueOf(id));
            sqlSession.commit();
            //int delete = jdbcTools.delete("delete from stuhhh where id=?", id);
            if (delete>0){
                List<Student> list = studentDao.findAll();
                //  List<Student> list = jdbcTools.selectall("select * from stuhhh", Student.class);
                req.setAttribute("list",list);
                req.getRequestDispatcher("showlist4.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
